<template>
  <div>
    <basic-container>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>数据</el-breadcrumb-item>
        <el-breadcrumb-item>产品</el-breadcrumb-item>
        <el-breadcrumb-item>实时新增数据（小时）</el-breadcrumb-item>
      </el-breadcrumb>
    </basic-container>
    <basic-container>
      <avue-crud
        :key="indexOde"
        ref="crud"
        :data="data"
        v-model="form"
        :option="option"
        :page.sync="page"
        @on-load="getList"
        @refresh-change="refreshChange"
        @search-reset="form = {}"
        @search-change="searchChange"
      >
      <!-- 自定义按钮 -->
      <template slot-scope="" slot="menuLeft">
        <el-button
          type="primary"
          icon="el-icon-download"
          size="small"
          plain
          @click="handleExcel()"
          >导出Excel</el-button
        >
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { common } from "@/api/cat/httpRequest";
import { productData as baseURL } from "@/api/cat/baseUrls";
export default {
  data() {
    return {
      page: {
        total: 0,
        pageSize: 10,
      },
      indexOde: "",
      type: true,
      data: [],
      params: {},
      form: {},
      option: {
        addBtn: false,
        editBtn: false,
        viewBtn: false,
        delBtn: false,
        excelBtn: false,
        menu: false,
        border: true,
        column: [
          {
            label: "日期",
            prop: "create_time",
            type: 'datetimerange',
            format:'yyyy-MM-dd HH:mm:ss',
            valueFormat:'yyyy-MM-dd HH:mm:ss',
            startPlaceholder: '时间日期开始范围自定义',
            endPlaceholder: '时间日期结束范围自定义',
            search: true,
            fixed: "left",
          },
          {
            label: "渠道",
            prop: "channel_id",
            type: "input",
            fixed: "left",
            search: true,
          },
          {
            label: "新增用户数",
            prop: "today_new_user_num",
            type: "input",
            fixed: "left",
          }
        ],
      },
    };
  },

  methods: {
    //数据导出
  async handleExcel() {
      this.loading = true;
      //处理搜索条件
      let params = { ...this.$refs.crud.searchForm };
      params.route = "数据/产品/实时新增数据（小时）";

      let export_condition = "";
      let userInfo = this.$store.state.user.userInfo;
      params.export_user_id = userInfo.id;
      params.export_user_name = userInfo.username;
      params.pageNum = 1;
      params.pageSize = 1000; 
      console.log(params)
      if(params.create_time.length>0){
        export_condition += " &时间区间："+params.create_time[0].toString()+"~"+params.create_time[1].toString()
        params.startTime = params.create_time[0];
        params.endTime = params.create_time[1];
      }
      if(params.channel_id){
        export_condition += " &渠道："+params.channel_id;
      }

      params.export_condition = export_condition == "" ? "所有" : export_condition;

     //调用导出接口
      let response = await common(
        baseURL.exportNewUserByHourStatistics,
        { ...params },
        null,
        "POST"
      );
      this.loading = false;
    },
    /**获取列表 */
    async getList() {
      this.loading = true;
      //清空表格列表
      this.data = [];
      //发送请求获取数据
      let response = await common(
        baseURL.getNewUserByHourStatistics,
        { ...this.params },
        this.page,
        "POST"
      );
      response.data.forEach((element, index) => {
        if(!element.hasOwnProperty('retentionInfo') || element.retentionInfo.length == 0){
          element.retentionInfo = [
              {
                id: element.id,
                n_column: 2,
                retention: "0",
                user_id: []
              }
          ]
        }
    });
      this.data = response.data;
      this.page = response.page;
      this.loading = false;
    },
    /**刷新 */
    refreshChange() {
      this.getList();
    },
    /**搜索 */
    searchChange(params, done) {
      if(params.create_time){
        params.startTime = params.create_time[0]
        params.endTime = params.create_time[1]
      }
 
      this.params = params;
      this.page.currentPage = 1;
      this.getList();
      if (done) done();
    },
  },
};
</script>